package br.ufc.cloud;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;

public class CleanDB extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.getWriter().print("<html>");
		resp.getWriter().print("<body>");
		
		
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		Query query = new Query("User");
		List<Entity> customers = datastore.prepare(query).asList(
				FetchOptions.Builder.withDefaults());
		
		if(customers.size() == 0)
		{
			resp.getWriter().print("A Agenda está vazia, não há o que apagar!");
		}
		
		for (Entity entity : customers) {
			resp.getWriter().print("Deleting...");
			resp.getWriter().print("Key deleted: " + entity.getKey());
			datastore.delete(entity.getKey());
		}
		
		resp.getWriter().print("</body>");
		resp.getWriter().print("</html>");
		
	}

}
